package cn.hx.mall.sell.controller;

import cn.hx.mall.sell.entity.SmsFlashPromotionSession;
import cn.hx.mall.sell.service.SmsFlashPromotionSessionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 限时购场次表(SmsFlashPromotionSession)表控制层
 *
 * @author makejava
 * @since 2022-11-02 16:10:30
 */
@Controller
@RequestMapping("sell/flashPromotionSession")
public class SmsFlashPromotionSessionController {
    /**
     * 服务对象
     */
    @Resource
    private SmsFlashPromotionSessionService flashPromotionSessionService;

    /**
     * 秒杀时段列表主页
     *
     * @param map 列表时段数据模型
     * @return 秒杀时段列表页面
     */
    @GetMapping("/list")
    public String list(ModelMap map) {
        List<SmsFlashPromotionSession> flashPromotionSessionList = flashPromotionSessionService.queryAll(null);
        map.addAttribute("flashPromotionSessions", flashPromotionSessionList);
        return "sell/flashPromotionSessionList";
    }

    /**
     * 根据秒杀时段的id查询，用于回显数据
     *
     * @param id 秒杀时段的id
     * @return 回显数据
     */
    @GetMapping("/queryById/{id}")
    @ResponseBody
    public Map<String, Object> queryById(@PathVariable Long id) {
        SmsFlashPromotionSession flashPromotionSession = flashPromotionSessionService.queryById(id);
        Map<String, Object> byId = new HashMap<>(1);
        byId.put("data", flashPromotionSession);
        System.out.println("回显数据==>" + byId);
        return byId;
    }

    /**
     * 保存（新增或者修改）
     *
     * @param flashPromotionSession 需要保存的数据
     * @return 重定向到秒杀时段列表
     */
    @PostMapping("/save")
    public String save(SmsFlashPromotionSession flashPromotionSession) {
        Long id = flashPromotionSession.getId();
        if (id == null) {
            flashPromotionSessionService.insert(flashPromotionSession);
        } else {
            flashPromotionSessionService.update(flashPromotionSession);
        }
        return "redirect:/sell/flashPromotionSession/list";
    }

    /**
     * 状态修改
     *      该方法已被取代，因为会导致页面端出现刷新跳转的抖动，用户体验不好
     *
     * @param flashPromotionSession  包含主键id及当前的启用状态
     * @return 重定向到秒杀时段列表
     * @since v1.0
     */
    @Deprecated
    @GetMapping("/status")
    public String status(SmsFlashPromotionSession flashPromotionSession) {
        if (flashPromotionSession.getStatus() == 1) {
            flashPromotionSession.setStatus(0);
        }else {
            flashPromotionSession.setStatus(1);
        }
        flashPromotionSessionService.update(flashPromotionSession);
        return "redirect:/sell/flashPromotionSession/list";
    }

    /**
     * 状态修改
     *
     * @param flashPromotionSession  需要修改的场次(id)
     * @return  场次id和修改后的状态
     */
    @PostMapping("/change")
    @ResponseBody
    public Map<String, Object> change(SmsFlashPromotionSession flashPromotionSession) {
        Integer curr = flashPromotionSessionService.queryById(flashPromotionSession.getId()).getStatus();
        if (curr == 1) {
            flashPromotionSession.setStatus(0);
        }else {
            flashPromotionSession.setStatus(1);
        }
        SmsFlashPromotionSession update = flashPromotionSessionService.update(flashPromotionSession);
        Map<String, Object> status = new HashMap<>();
        status.put("id", update.getId());
        status.put("status", update.getStatus());
        return status;
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return 重定向到秒杀时段列表
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        flashPromotionSessionService.deleteById(id);
        return "redirect:/sell/flashPromotionSession/list";
    }
}